package com.workplan.client.widgets.historyitem;

import com.general.client.action.ActionHandler;
import com.general.client.basewidget.BaseWidget;
import com.general.client.modes.WidgetMode;
import com.general.client.widgets.button.red.RedButton;
import com.general.client.widgets.textbox.TextBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;

public class HistoryItem extends BaseWidget implements
		HistoryItemPresenter.Display {

	private static EmployeeItemUiBinder uiBinder = GWT
			.create(EmployeeItemUiBinder.class);

	interface EmployeeItemUiBinder extends UiBinder<Widget, HistoryItem> {
	}

	@UiField
	TextBox date;
	@UiField
	TextBox totalTime;
	@UiField
	TextBox startTime;
	@UiField
	TextBox endTime;

	@UiField
	RedButton removeButton;

	public HistoryItem() {
		initWidget(uiBinder.createAndBindUi(this));
		initTextItem(date, false);
		initTextItem(totalTime, false);
		initTextItem(startTime, true);
		initTextItem(endTime, true);

		removeButton.getText().setText("Remove");

	}

	private void initTextItem(TextBox item, boolean editable) {
		item.setShowLabel(false);
		item.setOnBlurSwitchToView(editable);
		item.setOnClickSwitchToEdit(editable);
		item.setMandatory(true);
		item.setValidateOnValueChange(true);
		item.setMode(WidgetMode.VIEW);
	}

	@Override
	public HandlerRegistration addRemoveActionHandler(ActionHandler handler) {
		return removeButton.addActionHandler(handler);
	}

	@Override
	public HasText getDate() {
		return new HasText() {

			@Override
			public void setText(String text) {
				date.setValue(text);
			}

			@Override
			public String getText() {
				return date.getValue();
			}
		};
	}

	@Override
	public HasText getTotalTime() {
		return new HasText() {

			@Override
			public void setText(String text) {
				totalTime.setValue(text);
			}

			@Override
			public String getText() {
				return totalTime.getValue();
			}
		};
	}

	@Override
	public HasText getStartTime() {
		return new HasText() {

			@Override
			public void setText(String text) {
				startTime.setValue(text);
			}

			@Override
			public String getText() {
				return startTime.getValue();
			}
		};
	}

	@Override
	public HandlerRegistration addStartTimeChangeHandler(
			ValueChangeHandler<String> handler) {
		return startTime.addValueChangeHandler(handler);
	}

	@Override
	public HasText getEndTime() {
		return new HasText() {

			@Override
			public void setText(String text) {
				endTime.setValue(text);
			}

			@Override
			public String getText() {
				return endTime.getValue();
			}
		};

	}

	@Override
	public HandlerRegistration addEndTimeChangeHandler(
			ValueChangeHandler<String> handler) {
		return endTime.addValueChangeHandler(handler);
	}
}
